package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.exception.R;
import com.hqyj.pojo.GoodsIndex;
import com.hqyj.pojo.GoodsInfo;
import com.hqyj.dao.GoodsInfoMapper;
import com.hqyj.pojo.GoodsQueryParameter;
import com.hqyj.service.GoodsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2023-02-03 09:50:32
 */
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {

    @Resource
    GoodsInfoMapper goodsInfoMapper;

    //注入redis
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Override
    public R queryGoodsInfoForm() {

        //设置键名
        String key = "goods:index";
        //获取redis缓存数据
        Object obj = redisTemplate.opsForList().range(key,0,-1);
        List<GoodsIndex>  list =null;
        List<Object>  objList = (List<Object>)obj;
        //判断缓存是否有数据
        if(objList.size()==0){
            //查询数据库
             list = goodsInfoMapper.queryGoodsInfoForm();
             //添加到缓存中
             redisTemplate.opsForList().leftPushAll(key,list);
             //设置缓存失效时间
             redisTemplate.opsForList().getOperations().expire(key,15, TimeUnit.MINUTES);

        }else{
            list =( List<GoodsIndex>) objList.get(0);
        }
        return R.ok("success",list);


    }

    @Override
    public R queryGoodsInfoByGoodsTypeId(GoodsQueryParameter g) {

        //定义查询缓存键
        String key ="goods:detail:"
                +g.getGoodsTypeId()+":"
                +g.getBrandTypeId()+":"
                +g.getStartPrice()+":"
                +g.getEndPrice()+":"
                +g.getColName()+":"
                +g.getSortType()+":"
                +g.getPage()+":"
                +g.getSize()+":";
        //判断缓存是否有数据
        Object obj = redisTemplate.opsForHash().get(key,"list");
        //创建map对象
        HashMap<String,Object> map = new HashMap<>();
        if(obj==null){
            //1 创建分对象
            Page<GoodsInfo> page = new Page<>(g.getPage(),g.getSize());
            //2 查询数据库分页
            Page<GoodsInfo> infoPage = goodsInfoMapper.queryGoodsInfoByGoodsTypeId(page,g);

            //记录当前页集合
            map.put("list",infoPage.getRecords());
            //总页数
            map.put("totalPage",infoPage.getPages());
            //总条数
            map.put("total",infoPage.getTotal());
            //存入redis缓存
            redisTemplate.opsForHash().put(key,"list",infoPage.getRecords());
            redisTemplate.opsForHash().put(key,"totalPage",infoPage.getPages());
            redisTemplate.opsForHash().put(key,"total",infoPage.getTotal());
            //设置缓存的时长
            redisTemplate.opsForHash().getOperations().expire(key,15,TimeUnit.MINUTES);
        }else{
            //从缓存读取数据
            List<GoodsInfo> list = (List<GoodsInfo>)redisTemplate.opsForHash().get(key,"list");
            Integer totalPage = (Integer)redisTemplate.opsForHash().get(key,"totalPage");
            Integer total = (Integer)redisTemplate.opsForHash().get(key,"total");
            //记录当前页集合
            map.put("list",list);
            //总页数
            map.put("totalPage",totalPage);
            //总条数
            map.put("total",total);
        }

        return R.ok("success",map);
    }
}
